package com.smooware.icms.fragment.wo;

import android.os.Bundle;
import android.support.v4.app.ActionBar;
import android.support.v4.app.ActionBar.Tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

import com.smooware.icms.R;
import com.smooware.icms.adapter.pager.WOShedulesPagerAdapter;

public class WOShedulesFragment extends Fragment {

	private static final String TAG = WOShedulesFragment.class.getSimpleName();

	private static final String KEYS = "Keys";
	private static final String INVENTORY = "Inventory";
	private static final String WORK_ORDERS = "WOs";

	private WOShedulesPagerAdapter pagerAdapter;

	private ViewPager viewPager;
	private ActionBar actionBar;

	private PageAndTabsNavigator tabNavigator;

	private View previousButton;
	private View nextButton;

	@Override
	public View onCreateView(final LayoutInflater inflater,
			final ViewGroup container, final Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		View view = inflater.inflate(R.layout.shedules_fragment, container,
				false);

		Log.d(TAG, "onCreateView");

		viewPager = (ViewPager) view.findViewById(R.id.shedules_view_pager);

		previousButton = view.findViewById(R.id.date_navigation_left);
		nextButton = view.findViewById(R.id.date_navigation_right);

		return view;
	}

	@Override
	public void onActivityCreated(final Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		if (pagerAdapter == null) {
			pagerAdapter = new WOShedulesPagerAdapter(getActivity());
		}

		tabNavigator = new PageAndTabsNavigator();

		viewPager.setAdapter(pagerAdapter);
		viewPager.setOnPageChangeListener(tabNavigator);

		initActionBar();

		setOnClickActions();
	}

	private void initActionBar() {
		actionBar = getSupportActivity().getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		ActionBar.Tab woTab = actionBar.newTab().setText(WORK_ORDERS);
		woTab.setIcon(R.drawable.tab_wo);
		woTab.setTabListener(tabNavigator);
		actionBar.addTab(woTab);

		ActionBar.Tab inventoryTab = actionBar.newTab().setText(INVENTORY);
		inventoryTab.setIcon(R.drawable.tab_inv);
		inventoryTab.setTabListener(tabNavigator);
		actionBar.addTab(inventoryTab);

		ActionBar.Tab keysTab = actionBar.newTab().setText(KEYS);
		keysTab.setIcon(R.drawable.tab_key);
		keysTab.setTabListener(tabNavigator);
		actionBar.addTab(keysTab);
	}

	private void setOnClickActions() {
		previousButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				Toast.makeText(getActivity(), WORK_ORDERS, Toast.LENGTH_SHORT)
						.show();
			}
		});

		nextButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				Toast.makeText(getActivity(), WORK_ORDERS, Toast.LENGTH_SHORT)
						.show();
			}
		});
	}

	private class PageAndTabsNavigator implements
			ViewPager.OnPageChangeListener, ActionBar.TabListener {

		@Override
		public void onTabReselected(final Tab tab, final FragmentTransaction ft) {
		}

		@Override
		public void onTabSelected(final Tab tab, final FragmentTransaction ft) {
			viewPager.setCurrentItem(tab.getPosition());
		}

		@Override
		public void onTabUnselected(final Tab tab, final FragmentTransaction ft) {
		}

		@Override
		public void onPageScrolled(final int position,
				final float positionOffset, final int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(final int position) {
			actionBar.setSelectedNavigationItem(position);
		}

		@Override
		public void onPageScrollStateChanged(final int state) {
		}

	}
}
